class DocumentsController < ApplicationController
  # GET /documents
  # GET /documents.json
  before_filter :authenticate_employee!, :except => [:show_document, :download, :upload, :upload_doc]
  def index
    @documents = Document.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @documents }
    end
  end

  # GET /documents/1
  # GET /documents/1.json
  def show
    @document = Document.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @document }
    end
  end

  # GET /documents/new
  # GET /documents/new.json
  def new
    @document = Document.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @document }
    end
  end

  # GET /documents/1/edit
  def edit
    @document = Document.find(params[:id])
  end

  # POST /documents
  # POST /documents.json
  def create
    @document = Document.new(params[:document])

    respond_to do |format|
      if @document.save
        format.html { redirect_to @document, notice: 'Document was successfully created.' }
        format.json { render json: @document, status: :created, location: @document }
      else
        format.html { render action: "new" }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /documents/1
  # PUT /documents/1.json
  def update
    @document = Document.find(params[:id])

    respond_to do |format|
      if @document.update_attributes(params[:document])
        format.html { redirect_to @document, notice: 'Document was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.json
  def destroy
    @document = Document.find(params[:id])
    @document.destroy

    respond_to do |format|
      format.html { redirect_to documents_url }
      format.json { head :no_content }
    end
  end
  
  def download

     if current_client
      @document = Document.find(params[:id])
    client = Client.find(current_client)
   new_path =  "#{Rails.root}/public" + "/#{client.id}" +  "/"+@document.attachment_file_name
   send_file new_path, :type => @document.attachment_content_type, :disposition => 'attachment'
    elsif (current_employee.is_manager == false)
    @document = ClientDocument.find(params[:id])
  new_path =  "#{Rails.root}/public" + "/#{@document.client_id}" +  "/"+@document.attachment_file_name
    send_file new_path, :type => @document.attachment_content_type, :disposition => 'attachment'
   else
    @document = Document.find(params[:id])
    send_file @document.attachment.path, :type => @document.attachment_content_type, :disposition => 'attachment'
  end

  end

  def upload
   if current_employee
    @document = ClientDocument.find(params[:id])

    @new_path =  "#{Rails.root}/public" + "/#{@document.client_id}" +  "/"+@document.attachment_file_name
   else
    @document = Document.find(params[:id])
    client = Client.find(current_client)
   @new_path =  "#{Rails.root}/public" + "/#{client.id}" +  "/"+@document.attachment_file_name
end
  

  end

  def upload_doc
   if current_employee
    @document = ClientDocument.find(params[:id])
    document_attach = ClientDocument.where("document_id = ?",@document.document_id)
    # new_path =  "#{Rails.root}/public" + "/#{client.id}" +  "/"+ @document.attachment_file_name
    if !params[:client_document][:new_path].nil? && !current_employee.nil?
      #params[:document][:status] = "Pending"
      client_document = ClientDocument.where("client_id = ? and document_id = ? and status = 'Incomplete' or status  = 'Needs review' or status = 'Pending' ",@document.client_id, @document.document_id ).first
      client_document.status = "Pending"
      client_document.save
    upload_documents = Hash.new
    upload_documents = {:client_id=>params[:client_document][:client_id],:attachment_file_name=>params[:client_document][:attachment].original_filename,:attachment_content_type =>	params[:client_document][:attachment].content_type }
      document_attach.first.update_attributes(upload_documents)
 
   File.open(params[:client_document][:new_path], "wb") { |f| f.write(params[:client_document][:attachment].read) }
 
 #    DocumentNotification.doc_pending(client_document,current_client).deliver
      redirect_to show_document_path
    else
      redirect_to :back
    end

else

  @document = Document.find(params[:id])
    document_attach = ClientDocument.where("document_id = ?",@document.id)
    client = Client.find(current_client)
  # new_path =  "#{Rails.root}/public" + "/#{client.id}" +  "/"+ @document.attachment_file_name
   if !params[:document][:new_path].nil? && !current_client.nil?
      #params[:document][:status] = "Pending"
     client_document = ClientDocument.where("client_id = ? and document_id = ?",current_client.id, @document.id ).first
      client_document.status = "Pending"
      client_document.save
    upload_documents = Hash.new
  upload_documents = {:client_id=>params[:document][:client_id],:attachment_file_name=>params[:document][:attachment].original_filename,:attachment_content_type =>	params[:document][:attachment].content_type }
      document_attach.first.update_attributes(upload_documents)

  File.open(params[:document][:new_path], "wb") { |f| f.write(params[:document][:attachment].read) }

     DocumentNotification.doc_pending( @document,current_client).deliver
      redirect_to show_document_path
    else
      redirect_to :back
    end
end
  end

  def show_document
    @documents = get_document
  end

  def show_review
    @client_doc = ClientDocument.find(params[:id])
    @document = Document.find(@client_doc.document_id)
  end

  def review_save
    document = ClientDocument.find(params[:id])
    if params[:doc_status] == "review"
      document.status = "Needs review"
      client = Client.find(document.client_id)
      DocumentNotification.doc_review(Document.find(document.document_id),client,params[:comment]).deliver
    else
      document.status = "Complete"
     client = Client.find(document.client_id)
      DocumentNotification.doc_complete(Document.find(document.document_id),client).deliver
    end
    document.save 
    redirect_to :root
  end
 
end
